//
//  JSResetPwdViewController.m
//  NiubilityProject
//
//  Created by Apple on 2017/11/17.
//  Copyright © 2017年 Apple. All rights reserved.
//

#import "JSResetPwdViewController.h"

@interface JSResetPwdViewController ()
@property (weak, nonatomic) IBOutlet QMUITextField *emailTF;
@property (weak, nonatomic) IBOutlet UILabel *decL;
@property (weak, nonatomic) IBOutlet UITextField *checkCodeTF;
@property (weak, nonatomic) IBOutlet UIButton *sendCheckCodeBtn;
@property (weak, nonatomic) IBOutlet QMUITextField *pwdTF;
@property (weak, nonatomic) IBOutlet QMUITextField *checkTF;
@property (weak, nonatomic) IBOutlet UIButton *confrimBtn;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *contentHeight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space1;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space2;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space3;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *space4;
@property (weak, nonatomic) IBOutlet UIView *pwdBgView;
@property (weak, nonatomic) IBOutlet UIView *pwdChcekView;

@end

@implementation JSResetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
}
- (void)didInitialized{
    [super didInitialized];
    self.view.backgroundColor = UIColorWhite;
    self.contentHeight.constant = kRealValue(40);
    self.decL.text = JSLocalizedString(@"checkdec");
    self.emailTF.placeholder = JSLocalizedString(@"registeraccount");
    self.checkCodeTF.placeholder = JSLocalizedString(@"checkCodeTF");
    self.pwdTF.placeholder = JSLocalizedString(@"registerapwd");
    self.checkTF.placeholder = JSLocalizedString(@"registeracheckpwd");
    [self.confrimBtn setTitle:JSLocalizedString(kLocalizableAlertOk) forState:UIControlStateNormal];
    [self.sendCheckCodeBtn setTitle:JSLocalizedString(@"sendcheckd") forState:UIControlStateNormal];
    self.space1.constant = self.space2.constant = self.space3.constant = kRealValue(20);
    self.space4.constant = kRealValue(25);
    RACSignal *enableSignal = nil;
    if (!userManager.curUserInfo) {
        RACSignal *enableSignal1 = [[RACSignal combineLatest:@[self.emailTF.rac_textSignal,self.checkCodeTF.rac_textSignal,self.pwdTF.rac_textSignal,self.checkTF.rac_textSignal]] map:^id(id value) {
            NSString *emailStr = value[0];
            NSString *checkCodeStr = value[1];
            NSString *pwdStr   = value[2];
            NSString *checkStr = value[3];
            return @(JSRegularIsEmailQualified(emailStr) && checkCodeStr.length && pwdStr.length > 5 && pwdStr.length < 12 && [pwdStr isEqualToString:checkStr]);
        }];
        enableSignal = enableSignal1;
    }else{
        self.pwdBgView.hidden = YES;
        self.pwdChcekView.hidden = YES;
        RACSignal *enableSignal1 = [[RACSignal combineLatest:@[self.emailTF.rac_textSignal,self.checkCodeTF.rac_textSignal]] map:^id(id value) {
            NSString *emailStr = value[0];
            NSString *checkCodeStr = value[1];
            return @(JSRegularIsEmailQualified(emailStr) && checkCodeStr.length);
        }];
        enableSignal = enableSignal1;
    }
    self.confrimBtn.rac_command = [[RACCommand alloc] initWithEnabled:enableSignal signalBlock:^RACSignal *(id input) {
       
        return [RACSignal empty];
    }];
    
}
- (void)setNavigationItemsIsInEditMode:(BOOL)isInEditMode animated:(BOOL)animated{
    [super setNavigationItemsIsInEditMode:isInEditMode animated:animated];
    if (userManager.curUserInfo) {
        self.title = JSLocalizedString(@"bindingEmail");
    }else{
        self.title = JSLocalizedString(@"resetPwd");
    }
}
- (IBAction)sendCheckCodeClick:(UIButton *)sender {
    if (!JSRegularIsEmailQualified(self.emailTF.text)) {
        [JSAlertHelper showFailHideAfterDelay:2 text:JSLocalizedString(@"registeraccount") inView:self.view animation:YES];
        return;
    }
    sender.userInteractionEnabled = NO;
    __block UIButton *btn = sender;
    [sender ba_countDownWithTimeInterval:60 countDownFormat:@"%zd"];
    [sender setTimeStoppedCallback:^{
        [btn setTitle:JSLocalizedString(@"sendcheckd") forState:UIControlStateNormal];
    }];
    // 发送验证码
    [JSNetManager js_courrentProjectRequestWithMouth:@"forgetPsswordSendCode" cache:NO ifShowFinishStatu:NO parameters:@{@"email":self.emailTF.text}.mutableCopy successBlock:^(id response) {
        if ([response[@"status"] integerValue] == 200) {
            [JSAlertHelper showSucceedHideAfterDelay:1.5 text:JSLocalizedString(kLocalizableSuccess) inView:self.view animation:YES];
        }
    } failureBlock:nil];
}
- (IBAction)confirmClick:(UIButton *)sender {
    if (userManager.curUserInfo) { // 绑定邮箱
        
    }else{ //   重置密码
        
    }
}


@end
